package auth

import (
	"github.com/dgrijalva/jwt-go"
)


func GetUid(token string) (uint32, error) {
	claims := &Claims{}

	// Parse the JWT string and store the result in `claims`.
	// Note that we are passing the key in this method as well. This method will return an error
	// if the token is invalid (if it has expired according to the expiry time we set on sign in),
	// or if the signature does not match
	tkn, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
		return jwtKey, nil
	})

	if err != nil {
		if err == jwt.ErrSignatureInvalid {
			return 0, ErrUnauthorized
		}
		return 0, err
	}
	if !tkn.Valid {
		return 0, ErrUnauthorized
	}

	return claims.Uid, nil
}
